#include "XThread.h"
#include<thread>
using namespace std;


void XSleep(int mis)
{
    chrono::milliseconds du(mis);
    this_thread::sleep_for(du);
}
void XThread::SetPause(bool isP)
{
    isPause = isP;
    for(int i = 0;i<10;i++)
    {
        if(isPausing == isP)
        {
            break;
        }
        XSleep(10);
    }

}
bool XThread::Start()
{
    isExit = false;
    isPause = false;
    thread th(&XThread::ThreadMain,this);
    th.detach();
    return true;
}

void XThread::ThreadMain()
{
    isRuning = true;
    printf("threadMain etnter!");
    Main();
    printf("threadMain exit!");
    isRuning = false;
}

//通过控制isExit安全停止线程（不一定成功）
void XThread::Stop()
{
    isExit = true;
    for(int i = 0;i < 200 ;i++)
    {
        if(!isRuning){
            printf("Stor thread success");
            return;
        }
        XSleep(1);
    }
    printf("Stop thread overtime!");

}
